#include "PyCallBack.h"

PyObject* __docallback(PyObject* pCallObj, const char* format, va_list va)
{
	PyObject* args = NULL;
	if (format && strlen(format) == 1)
	{
		char fmt[32];
		sprintf(fmt, "(%c)", format[0]);

		args = Py_VaBuildValue(fmt, va);
	}
	else if (format && *format)
	{
		args = Py_VaBuildValue(format, va);
	}
	if (PyErr_Occurred()) 
		PyErr_Print();
	PyObject* ret = PyObject_CallObject(pCallObj, args);

	Py_XDECREF(args);

	if (PyErr_Occurred()) 
		PyErr_Print();
	return ret;
	
}


void CallPyMethod(PyObject* pCallObj, const char* format, ...)
{
	if (pCallObj == NULL || pCallObj == Py_None)
		return ;
	va_list va;
	va_start(va, format);
	PyObject* ret = __docallback(pCallObj, format, va);
	va_end(va);
	Py_XDECREF(ret);
}
PyObject* CallPyMethod_withRet(PyObject* pCallObj, const char* format, ...)
{
	if (pCallObj == NULL || pCallObj == Py_None)
		return NULL;
	va_list va;
	va_start(va, format);
	PyObject* ret = __docallback(pCallObj, format, va);
	va_end(va);
	return ret;
}